﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MultipleQueue.Simulation
{
    public class ServerSelection
    {
        public enum ServerSelectionTypes
        {
            HighestPriority,
            LowestUtilization,
            Random
        }
        private ServerSelectionTypes priorityType;

        public ServerSelection(ServerSelectionTypes priorityType)
        {
            this.priorityType = priorityType;
        }

        public int GetServerByPriority(List<Entities.Server> servers, float clock)
        {
            try
            {
                List<Entities.Server> idleServers = new List<Entities.Server>();
                if (this.priorityType == ServerSelectionTypes.Random)
                {
                    for (int i = 0; i < servers.Count; i++)
                    {
                        if (!servers[i].IsBusy)
                        {
                            idleServers.Add(servers[i]);
                        }
                    }
                    if (idleServers.Count > 0)
                    {
                        Random random = new Random(0);
                        return servers.IndexOf(idleServers[random.Next(idleServers.Count - 1)]);
                    }
                    else
                    {
                        return -1;
                    }
                }
                else if (this.priorityType == ServerSelectionTypes.HighestPriority)
                {
                    Entities.Server temp = new Entities.Server();
                    temp.Priority = -1;
                    int index = -1;

                    for (int i = 0; i < servers.Count; i++)
                    {
                        if (!servers[i].IsBusy && temp.Priority < servers[i].Priority)
                        {
                            temp = servers[i];
                            index = i;
                        }
                    }
                    return index;
                }
                else
                {
                    //Entities.Server temp = new Entities.Server();
                    float utilization = 1000;
                    int index = -1;
                    for (int i = 0; i < servers.Count; i++)
                    {
                        if (!servers[i].IsBusy && utilization >= servers[i].Utilization)
                        {
                            utilization = servers[i].Utilization;
                            index = i;
                        }
                    }
                    return index;
                }
            }
            catch
            {
                throw;
            }

            //check at least one server is not busy
            

            //switch (this.priorityType)
                //HighestPriority
                //Random
                //LowestUtilization
            //throw new NotImplementedException(@"Implement GetServerByPriority in Simulation\ServerSelection.cs");
        }
    }
}
